<?php
declare (strict_types = 1);

namespace app\wechat\controller;

use app\BaseController;
use app\Request;
use app\wechat\model\WxpayOrderModel;
use app\wechat\service\WxpayService;
use think\facade\View;

class Wxpay extends BaseController
{
    /**
     * 微信公众号支付
     *
     * @Author tanruizheng
     * @DateTime 2021-08-18
     * @return void
     */
    public function getJssdkPayConfig(){
        try {
            $wxpayService = new WxpayService('wx22650a4626757a41');
            $openid = 'okYDX50ByRlPu-H0XQ73cZk3Awf8';
            $outTradeNo = time().rand(10000,99999);
            $totalFee = 100;
            $body = '微信公众号支付';
            $payConfig = $wxpayService->getJssdkPayConfig($openid, $outTradeNo, $totalFee, $body);
            return json($payConfig);
        } catch (\Exception $e) {
            return json(self::createFail($e->getMessage()));
        }
    }

    /**
     * 微信小程序支付参数
     *
     * @Author tanruizheng
     * @DateTime 2021-08-19
     * @return void
     */
    public function getMiniPayConfig(){
        try {
            $wxpayService = new WxpayService('wx294975311d4ef718');
            $openid = 'oqqPl5UsxPOSzVXZF8jvsjlyrOtA';
            $outTradeNo = time().rand(10000,99999);
            $totalFee = 100;
            $body = '小程序支付';
            $payConfig = $wxpayService->getMiniPayConfig($openid, $outTradeNo, $totalFee, $body);
            return json($payConfig);
        } catch (\Exception $e) {
            return json(self::createFail($e->getMessage()));
        }
    }

    /**
     * 支付订单
     *
     * @Author tanruizheng
     * @DateTime 2021-08-25
     * @return void
     */
    public function orders(Request $request){
        if($request->param('_action') == 'getList'){
            $page = $request->param('page/d',1);
            $limit = $request->param('limit/d',15);
            $where = [];
            //列表
            $list = WxpayOrderModel::where($where)->page($page,$limit)->select()->toArray();
            foreach ($list as &$item) {
              
            }
            //总数
            $count = WxpayOrderModel::where($where)->count();
            return json(self::createList($list,$count));
        }
        return View::fetch('orders',[
           
        ]);
    }

}
